/**
 * 
 */
package kr.co.insoft.auth.repository.mail.impl;

import java.util.HashSet;

import kr.co.insoft.auth.mapper.mail.MaillingMapper;
import kr.co.insoft.auth.model.mail.MaillingFormModel;
import kr.co.insoft.auth.model.members.JoinFormModel;
import kr.co.insoft.auth.repository.mail.MaillingRepository;
import kr.co.insoft.framework.config.MAIL_TYPE;
import kr.co.insoft.framework.exceptions.ExceptionCode;
import kr.co.insoft.framework.exceptions.RepositoryException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.crypto.password.StandardPasswordEncoder;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;

/**
 * @author IN-SOFT Inc. GoodwillDD(sylee@in-soft.co.kr)
 * 
 */
@Repository
public class MaillingRepositoryImpl implements MaillingRepository {

	@Autowired
	MaillingMapper maillingMapper;

	@Override
	public void sendRegistedMail(JoinFormModel joinFormModel)
			throws RepositoryException {
		try {
			Assert.notNull(joinFormModel);
		} catch (Exception e) {
			throw new RepositoryException(ExceptionCode.ARGUMENT_ERROR);
		}

		try {
			MaillingFormModel maillingFormModel = new MaillingFormModel();
			maillingFormModel.setUserid(joinFormModel.getId());
			PasswordEncoder encoder = new StandardPasswordEncoder();
			maillingFormModel.setConfirmKey(encoder.encode(joinFormModel
					.toString()));
			maillingFormModel
					.setMailtype(MAIL_TYPE.MEMBER_ACCOUNT_ACCESS_ACCEPT
							.ordinal());
			maillingMapper.sendRegistedMail(maillingFormModel);
		} catch (Exception e) {
			throw new RepositoryException(ExceptionCode.REPOSITORY_EXCEPTION, e);
		}
	}

	@Override
	public HashSet<MaillingFormModel> checkMails() throws RepositoryException {
		try {
			return maillingMapper.checkMails();
		} catch (Exception e) {
			throw new RepositoryException(ExceptionCode.REPOSITORY_EXCEPTION, e);
		}
	}

	@Override
	public void completedMails(HashSet<MaillingFormModel> mails)
			throws RepositoryException {
		try {
			for (MaillingFormModel maillingFormModel : mails)
				maillingMapper.completedMails(maillingFormModel);
		} catch (Exception e) {
			throw new RepositoryException(ExceptionCode.REPOSITORY_EXCEPTION, e);
		}
	}

	@Override
	public void cleanMails() throws RepositoryException {
		try {
			maillingMapper.cleanMails();
		} catch (Exception e) {
			throw new RepositoryException(ExceptionCode.REPOSITORY_EXCEPTION, e);
		}
	}

	@Override
	public MaillingFormModel getMail(String confirmKey)
			throws RepositoryException {
		try {
			return maillingMapper.getMailByConfirmKey(confirmKey);
		} catch (Exception e) {
			throw new RepositoryException(ExceptionCode.REPOSITORY_EXCEPTION, e);
		}
	}

	@Override
	public void cleanMail(Long seq) throws RepositoryException {
		try {
			maillingMapper.cleanMail(seq);
		} catch (Exception e) {
			throw new RepositoryException(ExceptionCode.REPOSITORY_EXCEPTION, e);
		}
	}
}
